//
//  Toast.swift
//  Duyiguqin
//
//  Created by 张昭 on 2024/8/11.
//

import UIKit

class Toast: NSObject {
    
    private static var queue = [(String, TimeInterval)]()
    private static var isShowing = false
    private static let kToastViewTag: Int = 32382034343
    private static weak var window: UIView?

    private static func tryToShowNext() {
        cleanToastView()
        guard !isShowing, let message = queue.first?.0, let duration = queue.first?.1, let window = UIApplication.shared.windows.filter({ $0.isKeyWindow }).first else { return }
        self.window = window
        isShowing = true
        queue.removeFirst()
        
        let bgView = UIView()
        bgView.backgroundColor = UIColor.black
        bgView.layer.cornerRadius = 10
        bgView.layer.masksToBounds = true
        bgView.tag = kToastViewTag
        
        let toastLabel = UILabel()
        toastLabel.textColor = UIColor.white
        toastLabel.textAlignment = .center
        toastLabel.font = UIFont.systemFont(ofSize: 16.0)
        toastLabel.text = message
        toastLabel.alpha = 1.0
        toastLabel.numberOfLines = 0
        toastLabel.tag = kToastViewTag + 1

        let maxWidth = window.frame.size.width - 120
        let maxSizeTitle = CGSize(width: maxWidth, height: window.frame.size.height)
        var expectedSizeTitle = toastLabel.sizeThatFits(maxSizeTitle)
        expectedSizeTitle = CGSize(width: min(expectedSizeTitle.width, maxSizeTitle.width), height: min(expectedSizeTitle.height, maxSizeTitle.height))
        
        window.addSubview(toastLabel)
        toastLabel.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalTo(expectedSizeTitle.width)
            make.height.equalTo(expectedSizeTitle.height)
        }
        
        window.insertSubview(bgView, belowSubview: toastLabel)
        bgView.snp.makeConstraints { make in
            make.left.equalTo(toastLabel.snp.left).offset(-20)
            make.right.equalTo(toastLabel.snp.right).offset(20)
            make.top.equalTo(toastLabel.snp.top).offset(-10)
            make.bottom.equalTo(toastLabel.snp.bottom).offset(10)
        }

        UIView.animate(withDuration: 0.3, delay: duration, options: .curveEaseOut, animations: {
            toastLabel.alpha = 0.0
            bgView.alpha = 0.0
        }, completion: { _ in
            toastLabel.removeFromSuperview()
            bgView.removeFromSuperview()
            self.isShowing = false
            self.tryToShowNext()
        })
    }
    
    private static func cleanToastView() {
        guard let w = window else { return }
        _ = w.subviews.map({
            if $0.tag == kToastViewTag || $0.tag == kToastViewTag + 1 {
                $0.removeFromSuperview()
            }
        })
    }
}

extension Toast {
    
    @objc static func show(message: String) {
        guard !message.isEmpty else { return }
        queue.append((message, 2.0))
        tryToShowNext()
    }
    
    @objc static func show(message: String, duration: TimeInterval = 2.0) {
        guard !message.isEmpty else { return }
        queue.append((message, duration))
        tryToShowNext()
    }
    
    @objc static func clean() {
        cleanToastView()
    }
}
